package com.navi.loaddata.loadService.impl.EqEvent;

import com.navi.loaddata.core.errorLog.ErrorCode;
import com.navi.loaddata.core.etlException.ETLException2;
import com.navi.loaddata.core.mysqlEntity.EdaDiffD;
import com.navi.loaddata.core.mysqlEntity.EtlConfDModel;
import com.navi.loaddata.core.oracleEntity.EtlEquipEventV;
import com.navi.loaddata.core.oracleEntity.EtlEquipEventVMapper;
import com.navi.loaddata.core.repository.impl.MySqlRepositoryImpl;
import com.navi.loaddata.core.repository.impl.OracleRepositoryImpl;
import com.navi.loaddata.core.util.LoggerUtil;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author xufan
 */
@Service
@Transactional(transactionManager = "thirdaryPlatformTransactionManager",
        rollbackFor = {RuntimeException.class, Exception.class})
public class SaveEqState {
    private static final Logger logger = LoggerFactory.getLogger(SaveEqState.class);
    private LoggerUtil loggerUtil = new LoggerUtil(this.getClass());


    @Autowired
    private OracleRepositoryImpl dao3;


    public void saveEqStateList1000(EtlConfDModel etlConfD, Map<String, EtlEquipEventV> saveList) {
        Session session = dao3.getEntityManagerSession();
        String SQLEqEventProcedure = "";
        try {
            switch (etlConfD.getShop()) {
                case "ARRAY":
                    SQLEqEventProcedure = "{Call ARRAY_MES_LOADER.LOAD_EQUIP_EVENT()}";
                    for(String eq:saveList.keySet()){
                        dao3.save(EtlEquipEventVMapper.INSTANCE.etlEquipEventToArray(saveList.get(eq)));
                    }
                    break;
                case "CF":
                    SQLEqEventProcedure = "{Call CF_MES_LOADER.LOAD_EQUIP_EVENT()}";
                    for(String eq:saveList.keySet()){
                        dao3.save(EtlEquipEventVMapper.INSTANCE.etlEquipEventToCf(saveList.get(eq)));
                    }
                    break;
                case "OC":
                    if ("CELL1".equals(etlConfD.getCellType())) {
                        SQLEqEventProcedure = "{Call CELL1_MES_LOADER.LOAD_EQUIP_EVENT()}";
                        for(String eq:saveList.keySet()){
                            dao3.save(EtlEquipEventVMapper.INSTANCE.etlEquipEventToCell1(saveList.get(eq)));
                        }
                    }
                    break;
            }
        } catch (Exception e) {
            throw new ETLException2(ErrorCode.ERROR_SAVE_ORA_EQUIPEVENT, e);
        }


//         call proc
        try {
            SQLQuery queryDefect = session.createSQLQuery(SQLEqEventProcedure);
            queryDefect.executeUpdate();
        } catch (Exception e) {
            throw new ETLException2(ErrorCode.ERROR_PROCEDURE_ORA_EQUIPEVENT, e);
        }

    }

    public void deleteEqStateList1000(EtlConfDModel etlConfD) {

        Session session = dao3.getEntityManagerSession();
        String hqlEqState = "";

        try {
            switch (etlConfD.getShop()) {
                case "ARRAY":
                    hqlEqState = "Delete FROM EtlArrayEquipEventV";
                    break;
                case "CF":
                    hqlEqState = "Delete FROM EtlCfEquipEventV";
                    break;
                case "OC":
                    if ("CELL1".equals(etlConfD.getCellType())) {
                        hqlEqState = "Delete FROM EtlCellEquipEventV";
                    }
                    break;
            }
            Query qEqState = session.createQuery(hqlEqState);
            qEqState.executeUpdate();
        } catch (Exception e) {
            throw new ETLException2(ErrorCode.ERROR_DELETE_ORA_EQUIPEVENT, e);
        }
    }

}
